home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH18 / KEYCNT.ASM next >
Encoding:
Assembly Source File  |  1994-07-14  |  3.0 KB  |  131 lines

  1. ; This is an example of an active TSR that counts keyboard interrupts
  2. ; once activated.
  3.  
  4. ; The resident segment definitions must come before everything else.
  5.  
  6. ResidentSeg    segment    para public 'Resident'
  7. ResidentSeg    ends
  8.  
  9. EndResident    segment    para public 'EndRes'
  10. EndResident    ends
  11.  
  12.         .xlist
  13.         include     stdlib.a
  14.         includelib    stdlib.lib
  15.         .list
  16.  
  17.  
  18. ; Resident segment that holds the TSR code:
  19.  
  20. ResidentSeg    segment    para public 'Resident'
  21.         assume    cs:ResidentSeg, ds:nothing
  22.  
  23. ; The following variable counts the number of keyboard interrupts
  24.  
  25. KeyIntCnt    word    0
  26.  
  27. ; These two variables contain the original INT 9 and INT 16h
  28. ; interrupt vector values:
  29.  
  30. OldInt9        dword    ?
  31. OldInt16    dword    ?
  32.  
  33.  
  34. ; MyInt9-    The system calls this routine every time a keyboard
  35. ;        interrupt occus.  This routine increments the
  36. ;        KeyIntCnt variable and then passes control on to the
  37. ;        original Int9 handler.
  38.  
  39. MyInt9        proc    far
  40.         inc    ResidentSeg:KeyIntCnt
  41.         jmp    ResidentSeg:OldInt9
  42. MyInt9        endp
  43.  
  44.  
  45.  
  46.  
  47. ; MyInt16-    This is the passive component of this TSR.  An
  48. ;        application explicitly calls this routine with an
  49. ;        INT 16h instruction.  If AH contains 0FFh, this
  50. ;        routine returns the number of keyboard interrupts
  51. ;        in the AX register.  If AH contains any other value,
  52. ;        this routine passes control to the original INT 16h
  53. ;        (keyboard trap) handler.
  54.  
  55. MyInt16        proc    far
  56.         cmp    ah, 0FFh
  57.         je    ReturnCnt
  58.         jmp    ResidentSeg:OldInt16    ;Call original handler.
  59.  
  60. ; If AH=0FFh, return the keyboard interrupt count
  61.  
  62. ReturnCnt:    mov    ax, ResidentSeg:KeyIntCnt
  63.         iret
  64. MyInt16        endp
  65.  
  66.  
  67. ResidentSeg    ends
  68.  
  69.  
  70.  
  71. cseg        segment    para public 'code'
  72.         assume    cs:cseg, ds:ResidentSeg
  73.  
  74. Main        proc
  75.         meminit
  76.  
  77.         mov    ax, ResidentSeg
  78.         mov    ds, ax
  79.         mov    ax, 0
  80.         mov    es, ax
  81.  
  82.         print
  83.         byte    "Keyboard interrupt counter TSR program",cr,lf
  84.         byte    "Installing....",cr,lf,0
  85.  
  86. ; Patch into the INT 9 and INT 16 interrupt vectors.  Note that the
  87. ; statements above have made ResidentSeg the current data segment,
  88. ; so we can store the old INT 9 and INT 16 values directly into
  89. ; the OldInt9 and OldInt16 variables.
  90.  
  91.         cli                ;Turn off interrupts!
  92.         mov    ax, es:[9*4]
  93.         mov    word ptr OldInt9, ax
  94.         mov     ax, es:[9*4 + 2]
  95.         mov    word ptr OldInt9+2, ax
  96.         mov    es:[9*4], offset MyInt9
  97.         mov    es:[9*4+2], seg ResidentSeg
  98.  
  99.         mov    ax, es:[16h*4]
  100.         mov    word ptr OldInt16, ax
  101.         mov     ax, es:[16h*4 + 2]
  102.         mov    word ptr OldInt16+2, ax
  103.         mov    es:[16h*4], offset MyInt16
  104.         mov    es:[16h*4+2], seg ResidentSeg
  105.         sti                ;Okay, ints back on.
  106.  
  107. ; We're hooked up, the only thing that remains is to terminate and
  108. ; stay resident.
  109.  
  110.         print
  111.         byte    "Installed.",cr,lf,0
  112.  
  113.         mov    ah, 62h            ;Get this program's PSP
  114.         int    21h            ; value.
  115.  
  116.         mov    dx, EndResident        ;Compute size of program.
  117.         sub    dx, bx
  118.         mov    ax, 3100h        ;DOS TSR command.
  119.         int    21h
  120. Main        endp
  121. cseg        ends
  122.  
  123. sseg        segment    para stack 'stack'
  124. stk        db    1024 dup ("stack   ")
  125. sseg        ends
  126.  
  127. zzzzzzseg    segment    para public 'zzzzzz'
  128. LastBytes    db    16 dup (?)
  129. zzzzzzseg    ends
  130.         end    Main
  131.